Skip to content

Commit 85c927f

Browse files
committed
[Fix] Uint8Array.fromHex: avoid invoking toString on a non-string string argument
Also: - add test coverage from tc39/test262#3994
1 parent aae0940 commit 85c927f

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

Uint8Array.fromHex/implementation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ module.exports = function fromHex(string) {
1414
}
1515

1616
if (typeof string !== 'string') {
17-
throw new $TypeError('`string` is not a string: ' + string); // step 1
17+
throw new $TypeError('`string` is not a string: ' + typeof string); // step 1
1818
}
1919

2020
var result = FromHex(string); // step 2

test/Uint8Array.fromHex.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var defineProperties = require('define-properties');
44
var test = require('tape');
5+
var forEach = require('for-each');
6+
var getProto = require('es-abstract/helpers/getProto');
57

68
var index = require('../Uint8Array.fromHex');
79
var impl = require('../Uint8Array.fromHex/implementation');
@@ -52,6 +54,61 @@ module.exports = {
5254
'hex (' + hex + ') produces expected bytes (' + expected.join(', ') + ')'
5355
);
5456

57+
var illegal = [
58+
'a.a',
59+
'aa^',
60+
'a a',
61+
'a\ta',
62+
'a\x0Aa',
63+
'a\x0Ca',
64+
'a\x0Da',
65+
'a\u00A0a', // nbsp
66+
'a\u2009a', // thin space
67+
'a\u2028a' // line separator
68+
];
69+
forEach(illegal, function (value) {
70+
st['throws'](
71+
function () { method(value); },
72+
SyntaxError
73+
);
74+
});
75+
76+
var cases = [
77+
['', []],
78+
['66', [102]],
79+
['666f', [102, 111]],
80+
['666F', [102, 111]],
81+
['666f6f', [102, 111, 111]],
82+
['666F6f', [102, 111, 111]],
83+
['666f6f62', [102, 111, 111, 98]],
84+
['666f6f6261', [102, 111, 111, 98, 97]],
85+
['666f6f626172', [102, 111, 111, 98, 97, 114]]
86+
];
87+
forEach(cases, function (pair) {
88+
var arr = method(pair[0]);
89+
st.equal(getProto(arr), Uint8Array.prototype, 'decoding ' + pair[0]);
90+
st.deepEqual(arr, new Uint8Array(pair[1]), 'decoding ' + pair[0]);
91+
});
92+
93+
st.test('test262: test/built-ins/Uint8Array/fromHex/string-coercion.js', function (s2t) {
94+
var throwyToString = {};
95+
var results = s2t.intercept(
96+
throwyToString,
97+
'toString',
98+
{
99+
value: function () { throw new EvalError('toString called'); }
100+
}
101+
);
102+
103+
s2t['throws'](
104+
function () { method(throwyToString); },
105+
TypeError
106+
);
107+
s2t.deepEqual(results(), []);
108+
109+
s2t.end();
110+
});
111+
55112
st.end();
56113
});
57114
},

0 commit comments

Comments
 (0)